<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class request_transaksi_model extends CI_Model {

    private $pemakaian;

    function __construct() {
        parent::__construct();
    }

    function request_deposit($nominal_transfer, $pilihan_bank) {
        $id_member = $this->session->userdata('id_member');

        $sql = "select count(id_deposit) as jumlah from deposit where id_member=? and status in (0,-1)";
        $query = $this->db->query($sql, array($id_member));
        $result = $query->result();
        foreach ($result as $data) {
            $deposit_aktif = $data->jumlah;
        }
        if ($deposit_aktif > 5) {
            return "Deposit gagal dilakukan. Masih ada 5 deposit aktif. Silahkan transfer deposit tersebut atau tunggu masa expirednya berakhir (2 Jam).";
        }


        $nominal_transfer_asli = $nominal_transfer;
        // 1. Generate 3 digit angka
        $digit_unik = rand(1, 999);
        $nominal_transfer = substr($nominal_transfer_asli, 0, strlen($nominal_transfer_asli) - 3);
        $nominal_transfer = $nominal_transfer . $digit_unik;

        $percobaan = 1;
        $berhasil_deposit = false;
        // 2. Cek Availibilitas
        while ($percobaan < 99) {
            $sql = "select id_deposit from deposit where status in (0,-1) and nominal =?";
            $query = $this->db->query($sql, array($nominal_transfer));
            if ($query->num_rows() > 0) {
                $digit_unik = rand(1, 999);
                $nominal_transfer = substr($nominal_transfer_asli, 0, strlen($nominal_transfer_asli) - 3);
                $nominal_transfer = $nominal_transfer . $digit_unik;
                $percobaan++;
                continue;
            } else {
                $berhasil_deposit = true;
                break;
            }
        }

        // 3. Insert Ke Deposit
        if ($berhasil_deposit) {
            $query = "insert into deposit(id_member,nominal,bank) values(?,?,?)";
            $this->db->query($query, array($id_member, $nominal_transfer, $pilihan_bank));
            if (strtoupper($pilihan_bank) == "BCA") {
                $norek = "0620-3683-42";
                $an = "Akh Mirza Alief Syahrial";
            } else if (strtoupper($pilihan_bank) == "MANDIRI") {
                $norek = "1410-0127-60559";
                $an = "Akh Mirza Alief Syahrial";
            } else {
                return "Deposit gagal dilakukan, pilihan bank tidak tersedia";
            }
            $pukul = date('H:i', strtotime("now") + (60 * 60 * 2));
            $wording = "[BelanjaSMS.com] Silahkan transfer PERSIS sejumlah Rp. " . number_format($nominal_transfer) . " ke rek. " . strtoupper($pilihan_bank) . " no. $norek a.n $an sebelum pukul " . $pukul.". <br/><br/>Setelah transfer, <b>Konfirmasikan via SMS ke nomor 0822 4430 0195 dengan isi : KONFIRMASI ".strtoupper($pilihan_bank)." ".number_format($nominal_transfer)."</b>";

            // kirimkan notifikasi sms
            $sql = "select no_hp from member where id_member = ?";
            $query = $this->db->query($sql, array($id_member));
            $result = $query->result();
            foreach ($result as $data) {
                $phone = $data->no_hp;
            }

            if ($phone !== "") {
                $sql = "insert into message_outbox (receiver,content,date_created,id_member,is_sent) values (?,?,now(),?,0)";
                $this->db->query($sql, array($phone, $wording, $id_member));
            }

            return $wording;
        } else {
            // 4. Construct pesan
            return "Deposit gagal dilakukan, silahkan coba masukkan nominal lain (jika sebelumnya 10.000, coba masukkan 11.000, dst)";
        }
    }

    function tambah_request_smskit($jumlahPesan, $isiPesan, $idProduk, $penerima) {
        $this->load->model('produk_model');
        $produk = $this->produk_model->getDataProduk($idProduk);
        $idMember = $this->session->userdata('id_member');
        foreach ($produk->result() as $row) {
            $hpp = $row->hpp;
            $hargaJual = $row->harga_jual;
            $nominalCustom = $row->nominal_custom;
        }

        $data = array(
            $idMember,
            $idProduk,
            $hpp,
            $hargaJual,
            $nominalCustom,
            mysql_real_escape_string($jumlahPesan),
            substr(mysql_real_escape_string($isiPesan), 0, 160),
            $penerima,
            $this->session->userdata("data_port")
        );
        $query = "insert into request_transaksi(id_member,id_produk,hpp,harga_jual,nominal_custom,jumlah_beli,bill_info1,bill_info2,transaction_date,status,bill_info3) values (?,?,?,?,?,?,?,?,now(),0,?)";
        $this->db->query($query, $data);
        $id_request = $this->db->insert_id();
        $this->load->model('member_model');
        $this->member_model->updateSaldo((($hargaJual + $nominalCustom) * $jumlahPesan));
        return $id_request;
    }

    function tambah_request_smsfal($jumlahPesan, $isiPesan, $kota, $idProduk) {
        $this->load->model('produk_model');
        $produk = $this->produk_model->getDataProduk($idProduk);
        $idMember = $this->session->userdata('id_member');
        foreach ($produk->result() as $row) {
            $hpp = $row->hpp;
            $hargaJual = $row->harga_jual;
            $nominalCustom = $row->nominal_custom;
        }

        $data = array(
            $idMember,
            $idProduk,
            $hpp,
            $hargaJual,
            $nominalCustom,
            mysql_real_escape_string($jumlahPesan),
            substr(mysql_real_escape_string($isiPesan), 0, 160),
            mysql_real_escape_string($kota),
            "id_kota",
        );

        $query = "insert into request_transaksi(id_member,id_produk,hpp,harga_jual,nominal_custom,jumlah_beli,bill_info1,bill_info2,bill_info3,transaction_date) values (?,?,?,?,?,?,?,?,?,now())";
        $this->db->query($query, $data);
        $id_request = $this->db->insert_id();
        $this->load->model('member_model');
        $this->member_model->updateSaldo((($hargaJual + $nominalCustom) * $jumlahPesan));
        return $id_request;
    }

    function updateBillInfo($idRequest, $bill, $value) {
        $data = array("bill_info" . $bill => $value);
        $this->db->where("id_request", $idRequest);
        $this->db->update("request_transaksi", $data);
    }

    function updateStatus($idRequest, $status) {
        $data = array("status" => $status);
        $this->db->where("id_request", $idRequest);
        $this->db->update("request_transaksi", $data);
    }

    function deleteRequestTransaksi($idRequest) {
        $this->db->where('id_request', $idRequest);
        $this->db->delete('request_transaksi');
    }

    function getTransaksiTerbaru() {
//        $query = $this->db->query("select * from request_transaksi where id_member=" . $this->session->userdata('id_member') . " and (status = 0 or status = 1) and transaction_date >= date_sub(now(), interval 7 day) order by id_request desc limit 50");
        $sql = "select id_request,DATE_FORMAT(a.transaction_date,'%d %M %Y')tgl,a.id_produk,a.jumlah_beli,a.bill_info1 from request_transaksi a
where a.transaction_date > DATE_SUB(CURDATE(),INTERVAL 2 DAY) 
order by a.transaction_date desc
limit 50";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    function get_transaksi_detail($id) {
        $sql = "select receiver,status from flag_message_outbox where id_request={$id}";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

}

?>
